﻿using System;
using System.Collections;
using System.Collections.Generic;
using Web.App.Infrastructure.UseCases.Contracts;
using Web.App.Infrastructure.Context.Contracts;

namespace Web.App.Infrastructure.UseCases.Implementation
{
    public class UseCase : IUseCase
    {
        private readonly ICustomSessionState _sessionState;
        private const string USES_CASE_STACK ="useCaseStack";
        private const string INPUT_STACK = "inputStack";
        private const string OUTPUT_STACK = "outputStack";
        
        private Dictionary<string, object> UseCaseStack
        {
            get
            {
                if (_sessionState[USES_CASE_STACK] == null)
                    _sessionState[USES_CASE_STACK] = new Dictionary<string, object>();
                return _sessionState[USES_CASE_STACK] as Dictionary<string, object>;
            }
        }

        private Dictionary<string, object> InputStack
        {
            get
            {
                if (_sessionState[INPUT_STACK] == null)
                    _sessionState[INPUT_STACK] = new Dictionary<string, object>();
                return _sessionState[INPUT_STACK] as Dictionary<string, object>;
            }
        }

        private Dictionary<string, object> OutputStack
        {
            get
            {
                if (_sessionState[OUTPUT_STACK] == null)
                    _sessionState[OUTPUT_STACK] = new Dictionary<string, object>();
                return _sessionState[OUTPUT_STACK] as Dictionary<string, object>;
            }
        }

        public UseCase(ICustomSessionState _sessionState, ICustomApplicationState _applicationState)
        {
            this._sessionState = _sessionState;
        }

        public void InitialiseSubUseCase(object actionResultAfterFinish, object inputData, string useCaseKey)
        {
            InitialiseSubUseCase(actionResultAfterFinish, useCaseKey);
            InputStack[useCaseKey] = inputData;
        }

        public void InitialiseSubUseCase(object actionResultAfterFinish, string useCaseKey)
        {
            UseCaseStack[useCaseKey] = actionResultAfterFinish;
        }

        public object GetRedirectionPoint(string useCaseKey)
        {
            if (UseCaseStack.ContainsKey(useCaseKey))
                return UseCaseStack[useCaseKey];

            return null;
        }

        public object GetInputData(string useCaseKey)
        {
            if(InputStack.ContainsKey(useCaseKey))
                return  InputStack[useCaseKey];
            return null;
        }

        public void SetUseCaseResult(object output, string useCaseKey)
        {
            OutputStack[useCaseKey] = output;
        }

        public object GetUseCaseResult(string useCaseKey)
        {
            return OutputStack[useCaseKey];
        }
    }
}
